package com.lineage.server.timecontroller.pc;

import com.lineage.server.datatables.MapsTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_MapTimer;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MapTimerThread extends TimerTask {
  private static MapTimerThread _instance;
  
  private static final Log _log = LogFactory.getLog(MapTimerThread.class);
  
  public static final Map<L1PcInstance, Integer> TIMINGMAP = new ConcurrentHashMap<>();
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public static MapTimerThread getInstance() {
    if (_instance == null)
      (_instance = new MapTimerThread()).start(); 
    return _instance;
  }
  
  public static void put(L1PcInstance pc, int time) {
    pc.sendPackets((ServerBasePacket)new S_MapTimer(time));
    TIMINGMAP.put(pc, new Integer(time));
  }
  
  private void MapTimeCheck(L1PcInstance pc) {
    int mapid = pc.getMapId();
    int maxMapUsetime = MapsTable.get().getMapTime(mapid) * 60;
    int usedtime = pc.getMapUseTime(mapid);
    if (usedtime > maxMapUsetime) {
      teleport(pc);
    } else {
      usedtime++;
      message(pc);
      pc.setMapUseTime(mapid, usedtime);
      int leftTime = maxMapUsetime - usedtime;
      if (leftTime % 60 == 0)
        pc.sendPackets((ServerBasePacket)new S_MapTimer(leftTime)); 
    } 
  }
  
  private void message(L1PcInstance pc) {
    int map = pc.getMapId();
    int maxMapUsetime = MapsTable.get().getMapTime(map) * 60;
    int usedtime = pc.getMapUseTime(map);
    int lefttime = maxMapUsetime - usedtime;
    S_ServerMessage msg = null;
    switch (lefttime) {
      case 10800:
        msg = new S_ServerMessage(1526, String.valueOf(3));
        break;
      case 7200:
        msg = new S_ServerMessage(1526, String.valueOf(2));
        break;
      case 3600:
        msg = new S_ServerMessage(1526, String.valueOf(1));
        break;
      case 1800:
        msg = new S_ServerMessage(1527, String.valueOf(30));
        break;
      case 900:
        msg = new S_ServerMessage(1527, String.valueOf(15));
        break;
      case 600:
        msg = new S_ServerMessage(1527, String.valueOf(10));
        break;
      case 300:
        msg = new S_ServerMessage(1527, String.valueOf(5));
        break;
      case 120:
        msg = new S_ServerMessage(1527, String.valueOf(2));
        break;
      case 60:
        msg = new S_ServerMessage(1527, String.valueOf(1));
        break;
      case 10:
        msg = new S_ServerMessage(1528, String.valueOf(10));
        break;
      case 9:
        msg = new S_ServerMessage(1528, String.valueOf(9));
        break;
      case 8:
        msg = new S_ServerMessage(1528, String.valueOf(8));
        break;
      case 7:
        msg = new S_ServerMessage(1528, String.valueOf(7));
        break;
      case 6:
        msg = new S_ServerMessage(1528, String.valueOf(6));
        break;
      case 5:
        msg = new S_ServerMessage(1528, String.valueOf(5));
        break;
      case 4:
        msg = new S_ServerMessage(1528, String.valueOf(4));
        break;
      case 3:
        msg = new S_ServerMessage(1528, String.valueOf(3));
        break;
      case 2:
        msg = new S_ServerMessage(1528, String.valueOf(2));
        break;
      case 1:
        msg = new S_ServerMessage(1528, String.valueOf(1));
        break;
    } 
    pc.sendPackets((ServerBasePacket)msg);
  }
  
  public void run() {
    try {
      if (!TIMINGMAP.isEmpty()) {
        Iterator<L1PcInstance> iterator = TIMINGMAP.keySet().iterator();
        while (iterator.hasNext()) {
          L1PcInstance pc = iterator.next();
          if (pc == null || 
            pc.getNetConnection() == null)
            continue; 
          if (pc.isDead())
            continue; 
          if (!pc.isInTimeMap())
            continue; 
          MapTimeCheck(pc);
        } 
      } 
    } catch (Throwable e) {
      _log.error("计时地图时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      MapTimerThread MapTimer = new MapTimerThread();
      MapTimer.start();
    } 
  }
  
  private void teleport(L1PcInstance pc) {
    TIMINGMAP.remove(pc);
    if (World.get().getPlayer(pc.getName()) == null)
      return; 
    if (pc.isInTimeMap()) {
      int locx = 33427;
      int locy = 32822;
      int mapid = 4;
      int heading = 5;
      L1Teleport.teleport(pc, 33427, 32822, (short)4, 5, true);
    } 
  }
}
